<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2006 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */

/**
 * Used to log the operations in the admin.
 */
class Admin_Log extends Pluf_Model
{
    function init()
    {
        $this->_a['verbose'] = __('log entry');
        $this->_a['table'] = 'admin_logs';
        $this->_a['model'] = 'Admin_Log';
        $this->_a['cols'] = array(
                             // It is mandatory to have an "id" column.
                            'id' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Sequence',
                                  //It is automatically added.
                                  'blank' => true, 
                                  ),
                            'user' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Foreignkey',
                                  'model' => 'Pluf_User',
                                  'blank' => false,
                                  ),
                            'object_id' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Integer',
                                  'blank' => false,
                                  ),
                            'object_class' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Varchar',
                                  'blank' => false,
                                  'size' => 200,
                                  ),
                            'object_repr' =>
                            array('type' => 'Pluf_DB_Field_Varchar',
                                  'blank' => false,
                                  'size' => 200,
                                  ),
                            'action_flag' =>
                            array('type' => 'Pluf_DB_Field_Integer',
                                  'blank' => false,
                                  'verbose' => __('action'),
                                  'choices' => array(
                                                     1 => __('addition'),
                                                     2 => __('change'),
                                                     3 => __('deletion'),
                                                     ),
                                  ),
                            'change_message' =>
                            array('type' => 'Pluf_DB_Field_Text',
                                  'blank' => true,
                                  'verbose' => __('change message'),
                                  ),
                            'action_dtime' =>
                            array('type' => 'Pluf_DB_Field_Datetime',
                                  'blank' => true,
                                  ),
                                  );
        $this->_a['idx'] = array(                           
                            'object_id_idx' =>
                            array(
                                  'col' => 'object_id',
                                  'type' => 'normal',
                                  ),
                            'object_class_idx' =>
                            array(
                                  'col' => 'object_class',
                                  'type' => 'normal',
                                  ),
                            );
    }

    /**
     * String representation of the log entry.
     */
    function __toString()
    {
        return Pluf_Date::gmDateToString($this->action_dtime);
    }

    /**
     * Set the last_login and date_joined before creating.
     *
     */
    function preSave()
    {
        $this->action_dtime = gmdate('Y-m-d H:i:s');
    }

    public function is_addition()
    {
        return ($this->action_flag == 1);
    }

    public function is_change()
    {
        return ($this->action_flag == 2);
    }

    public function is_deletion()
    {
        return ($this->action_flag == 3);
    }

    public function get_edited_object()
    {
        return Pluf::factory($this->object_class, $this->object_id);
    }

    public function get_admin_url()
    {
        $app = $model = ''; // FIXME: Need to see the structure of the
                            // papp.php files to see how to easily get
                            // those values.
        return sprintf('%s/%s/%s/', $app, $model, $this->object_id);
    }

    /**
     * Log an action.
     */
    public static function logAction($user, $object, $action, $message='')
    {
        $log = new Admin_Log();
        $log->user = $user;
        $log->object_id = $object->id;
        $log->object_class = $object->_a['model'];
        $log->object_repr = (string) $object;
        $log->action_flag = $action;
        $log->change_message = $message;
        $log->create();
    }
}
